var mongoose = require("mongoose");
var fs = require("fs");
// 库存分页
module.exports.getInventoryList = async function (data) {
    
    var numbers = await mongoose.model("inventoryModel")
        .find({ merchantId: data.merchantId }).countDocuments();
    var maxNumbers = Math.ceil(numbers / data.pageSize);
    if (data.pageNumber > maxNumbers && maxNumbers != 0) {
        data.pageNumber = maxNumbers;
    }
    var rows = await mongoose.model("inventoryModel")
        .find({ merchantId: data.merchantId })
        .limit(data.pageSize - 0)
        .skip(data.pageSize * (data.pageNumber - 1))
    return { rows, maxNumbers }
}
// 获取全部库存
module.exports.getAllInventory = async function (data) {
   
    return await mongoose.model("inventoryModel").find({ merchantId: data.data });
}
//获取商品
module.exports.getcommodity = async function ({ several, yeshu }) {
    //获取当前集合的数据总条数
    const rowss = await mongoose.model('commodityModel').countDocuments()
    //计算最大页数
    const pages = Math.ceil(rowss / several);
    const data = await mongoose.model('commodityModel').find().limit(several - 0).skip((yeshu - 1) * several);
    return { data, rowss, pages, several, yeshu }
}
//添加商品
module.exports.addcommodity = async function ({ data1 }) {
    return await mongoose.model('commodityModel').create({
        _id: data1._id,
        commodityName: data1.commodityName,
        price: data1.price,
        image: data1.img,
        orgin: data1.orgin,
        package: data1.package,
        inventoryNumbers: data1.inventoryNumbers,
        amount: data1.amount,
        particularsImg: data1.particularsImg
    })
}
//删除商品
module.exports.deltcommodity = async function (data) {
    return await mongoose.model('commodityModel').deleteOne(data);
}
//显示删除商品
module.exports.updatecommodity = async function (data) {
    return await mongoose.model('commodityModel').find(data)
}
//修改商品
module.exports.submitcommodity = async function ({ data }) {
    // await mongoose.model('commodityModel').updateOne({ _id: data._id }, { 
    //     commodityName: data.commodityName, price: data.price, orgin: data.orgin, package: data.package, amount: data.amount, 
    //     inventoryNumbers: data.inventoryNumbers,image:data.image,particularsImg:data.particularsImg })

    await mongoose.model('commodityModel').updateOne({ _id: data._id }, data);
}
//查询商品
module.exports.inquirecommodity = async function ({ lei, leirong, several, yeshu }) {
    //获取当前集合的数据总条数
    const rowss = (await mongoose.model('commodityModel').find({ [lei]: leirong })).length;
    //计算最大页数
    const pages = Math.ceil(rowss / several);
    var data = await mongoose.model('commodityModel').find({ [lei]: leirong }).limit(several - 0).skip((yeshu - 1) * several);

    return { data, several, yeshu, rowss, pages }
}
// 修改库存
module.exports.updateInventory = async function (data) {
    return await mongoose.model("inventoryModel").update({ _id: data._id }, {
        ...data
    })
}
// 添加库存
module.exports.addInventory = async function (data) {
    return await mongoose.model("inventoryModel").create(data)
}
// 删除库存
module.exports.removeInventory = async function (data) {
    if (data.img && data.img != "暂无图片.jpg") {
        fs.unlink(`./public/images/${data.img}`, (err) => { console.log(err) })
    }
    if (data.particularsImg) {
        data.particularsImg.some((item) => {
            fs.unlink(`./public/detailsImages/${item}`, (err) => { console.log(err) })
        });
    }

    return await mongoose.model("inventoryModel").deleteOne({ _id: data._id })
}
module.exports.quireInventory = async function (data) {
    console.log(data)
    var numbers = await mongoose.model("inventoryModel")
        .find({ [data.searType]: { $regex: data.searValue, $options: '$i' } }).countDocuments();
    var maxNumbers = Math.ceil(numbers / data.pageSize);
    if (data.pageNumber > maxNumbers && maxNumbers != 0) {
        data.pageNumber = maxNumbers;
    }
    var rows = await mongoose.model("inventoryModel")
        .find({ [data.searType]: { $regex: data.searValue, $options: '$i' } })
        .limit(data.pageSize - 0)
        .skip(data.pageSize * (data.pageNumber - 1))
        ;
    return { rows, maxNumbers }
}
// 通过id查询库存
module.exports.getInventoryById = async function (data) {
    return mongoose.model("inventoryModel").findById(data)
}